package com.example.im

import android.text.TextUtils
import cc.vv.lkimcomponent.lkim.bean.LKIMConversation
import com.example.baselibrary.db.dao.DNDDao
import com.example.baselibrary.db.dao.IMTopDao
import com.example.baselibrary.db.table.IMTopTable
import com.example.util.finallist.IMExtKey
import com.example.util.finallist.SkKey
import java.util.*

/**
 * 会话列表初始化操作
 * @author  chaohaipeng  on 2018/7/24.
 */
class IMConversationOperate {
    private object mHolder {
        val instance = IMConversationOperate()
    }
    companion object {
        fun getInstance(): IMConversationOperate {
            return mHolder.instance
        }
    }

    /**
     * 初始化置顶
     *
     * @param lkimConversations 会话集合
     */
    fun initTopExt(lkimConversations: List<LKIMConversation>) {
        for (imConversation in lkimConversations) {
            imConversation.setAttribute(IMExtKey.EXTKEY_TOP, SkKey.BTKEY_String_0)
        }
        val imTopTables = IMTopDao.getInstance().queryAll()
        if (null != imTopTables && !imTopTables!!.isEmpty()) {
            for (imTopTable in imTopTables!!) {
                for (imConversation in lkimConversations) {
                    if (TextUtils.equals(imTopTable.conversationId, imConversation.conversationId)) {
                        imConversation.setAttribute(IMExtKey.EXTKEY_TOP, SkKey.BTKEY_String_1)
                    }
                }
            }
        }
        conversationsSort(lkimConversations)
    }
    /**
     * 初始化免打扰扩展属性
     *
     * @param lkimConversations
     */
    fun initDNDExt(lkimConversations: List<LKIMConversation>) {
        for (imConversation in lkimConversations) {
            imConversation.setAttribute(IMExtKey.EXTKEY_DND, SkKey.BTKEY_String_0)
        }
        val dndTables = DNDDao.getInstance().queryAll()
        if (null != dndTables && !dndTables!!.isEmpty()) {
            for (dndTable in dndTables!!) {
                for (imConversation in lkimConversations) {
                    if (TextUtils.equals(dndTable.conversationId, imConversation.conversationId)) {
                        imConversation.setAttribute(IMExtKey.EXTKEY_DND, SkKey.BTKEY_String_1)
                    }
                }
            }
        }
    }

    /**
     * 会话列表数据排序
     *
     * @param lkimConversations 会话集合
     */
    fun conversationsSort(lkimConversations: List<LKIMConversation>) {
        Collections.sort(lkimConversations) { imc1, imc2 ->
            var imSort = 0
            try {
                val imc1IsStick = imc1.getAttribute(IMExtKey.EXTKEY_TOP, SkKey.BTKEY_String_0)
                val imc2IsStick = imc2.getAttribute(IMExtKey.EXTKEY_TOP, SkKey.BTKEY_String_0)
                if (TextUtils.equals(imc1IsStick, imc2IsStick)) {
                    if (imc1.time > imc2.time) {
                        imSort = -1
                    } else if (imc1.time < imc2.time) {
                        imSort = 1
                    }
                } else if (SkKey.BTKEY_String_1.equals(imc1IsStick)) {
                    imSort = -1
                } else {
                    imSort = 1
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

            imSort
        }
    }

    /**
     * 编辑置顶  设置、取消
     *
     * @param lkimConversation 会话
     */
    fun editTopExt(lkimConversation: LKIMConversation) {
        val topTable = IMTopDao.getInstance().queryById(lkimConversation.conversationId)
        if (null == topTable) {
            IMTopDao.getInstance().insert(IMTopTable(lkimConversation.conversationId))
            lkimConversation.setAttribute(IMExtKey.EXTKEY_TOP, SkKey.BTKEY_String_1)
        } else {
            IMTopDao.getInstance().deleteById(lkimConversation.conversationId)
            lkimConversation.setAttribute(IMExtKey.EXTKEY_TOP, SkKey.BTKEY_String_0)
        }
    }
}